'use strict';

const Service = require('egg').Service;
const ulid = require('../public/ulid');
const ConnectOption = require('../oltmodules/ConnectOption');
const ZTEOLT = require('../oltmodules/ZTEOLT');
const HWOLT = require('../oltmodules/HuaWeiOLT');
const XDOLT = require('../oltmodules/XinDeOLT');
const XGLOLT = require('../oltmodules/XinGeLinOLT');
class DbaProfileService extends Service {
  async list(params) {
    let result = {
      count:"",
      rows:""
    }
    const where = {};
    where.oltId = params.OltId
    if(params.search){
      where.name = new RegExp(params.search)
    }
    result.count = await this.ctx.model.DbaProfile.find(where).count('_id')
    result.rows = await this.ctx.model.DbaProfile.find(where).collation({"locale": "zh", numericOrdering:true}).sort({name:1}).skip(parseInt((params.page - 1) * params.limit)).limit(parseInt(params.limit))
    return result
  }
  async allList(params) {
    let result = await this.ctx.model.DbaProfile.find({oltId:params.OltId}).collation({"locale": "zh", numericOrdering:true}).sort({name:1})
    return result
  }
  async create(params) {
    let result = {
      msg:"",
      flag:false
    }
    try {
      let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltId})
      if(oltResult.oltType == "HUAWEI"){
        params.fbw = Math.floor(parseInt(params.fbw)/64)*64
        params.abw = Math.floor(parseInt(params.abw)/64)*64
        params.max = Math.floor(parseInt(params.max)/64)*64
      }
      let dbaProfileResult = await this.ctx.model.DbaProfile.findOne({name:params.name,oltId:params.oltId})
      if(dbaProfileResult){
        result.msg = "create dbaprofile fail, this dbaprofile name is exist"
        result.flag = false
        return result
      }
      await dbaProfileCreate(oltResult,params,this)
      if(params.type == 1){
        params.abw = 0
        params.max = 0
      }
      if(params.type == 2){
        params.fbw = 0
        params.max = 0
      }
      if(params.type == 3){
        params.fbw = 0
      }
      if(params.type == 4){
        params.fbw = 0
        params.abw = 0
      }
      await this.ctx.model.DbaProfile.create({_id:ulid.ulid(),name:params.name,description:params.description,oltId:params.oltId,max:params.max,fbw:params.fbw,type:params.type,abw:params.abw})
      result.msg = "create dbaprofile successful"
      result.flag = true
      return result
    } catch (error) {
      result.msg = "create dbaprofile fail,"+error
      result.flag = false
      return result
    }
  }
  async edit(params) {
    let result = {
      msg:"",
      flag:false
    }
    try {
      let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltId})
      if(oltResult.oltType == "HUAWEI"){
        params.fbw = Math.floor(parseInt(params.fbw)/64)*64
        params.abw = Math.floor(parseInt(params.abw)/64)*64
        params.max = Math.floor(parseInt(params.max)/64)*64
      }
      await dbaProfileEdit(oltResult,params,this)
      if(params.type == 1){
        params.abw = 0
        params.max = 0
      }
      if(params.type == 2){
        params.fbw = 0
        params.max = 0
      }
      if(params.type == 3){
        params.fbw = 0
      }
      if(params.type == 4){
        params.fbw = 0
        params.abw = 0
      }
      await this.ctx.model.DbaProfile.updateOne({_id:params.id},{$set:{name:params.name,description:params.description,max:params.max,type:params.type,abw:params.abw,fbw:params.fbw}})
      result.msg = "edit dbaprofile successful"
      result.flag = true
      return result
    } catch (error) {
      result.msg = "edit dbaprofile fail,"+error,
      result.flag = false
      return result
    }
  }
  async delete(params) {
    let result = {
      msg:"",
      flag:false
    }
    try {
      let dbaProfileResult = await this.ctx.model.DbaProfile.findOne({_id:params.id})
      let oltResult = await this.ctx.model.Olt.findOne({_id:dbaProfileResult.oltId})
      await dbaProfileDelete(dbaProfileResult,oltResult,this)
      await this.ctx.model.DbaProfile.deleteOne({_id:params.id})
      result.msg = "delete dbaprofile successful"
      result.flag = true
      return result
    } catch (error) {
      result.msg = "delete dbaprofile fail,"+error,
      result.flag = false
      return result
    }
  }
}
async function dbaProfileCreate(oltInformation,params,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        let zte = new ZTEOLT(options)
        result = await zte.dbaProfileCreate(params.name,params.type,params.max,params.fbw,params.abw,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI":
        let hw = new HWOLT(options)
        result = await hw.dbaProfileCreate(params.name,params.type,params.max,params.fbw,params.abw,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}
async function dbaProfileEdit(oltInformation,params,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        let zte = new ZTEOLT(options)
        result = await zte.dbaProfileEdit(params.name,params.type,params.max,params.fbw,params.abw,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI":
        let hw = new HWOLT(options)
        result = await hw.dbaProfileEdit(params.name,params.type,params.max,params.fbw,params.abw,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}
async function dbaProfileDelete(dbaProfileResult,oltInformation,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        let zte = new ZTEOLT(options)
        result = await zte.dbaProfileDelete(dbaProfileResult.name,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI":
        let hw = new HWOLT(options)
        result = await hw.dbaProfileDelete(dbaProfileResult.name,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}

module.exports = DbaProfileService;
